window.appModule.controller('knowledgeControl', function ($scope,$http,$state,knowledgeAPI,$cookies,ngNotify,$document ) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    knowledgeAPI.getQuestionBasesByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.questionBases = result.questionBaseList
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    knowledgeAPI.getUserById({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.title = result.user.nickname;
            if(result.user.nickname.length > 8){
                result.user.nickname = result.user.nickname.substring(0,8)+"...";
            }else if(result.user.nickname.length == 0){
                result.user.nickname = result.user.phoneNumber;
            }
            if('' == result.user.imageUrl){
                result.user.imageUrl = "resource/images/UserAvatar.png";
            }
            $scope.user = result.user;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    knowledgeAPI.getMenusByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.menus = result.menuList;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goSecondMenu = function(menuId) {
        if(1 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("robotList");
                }
            });
        }else if(2 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("knowledgeList");
                }
            });
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $scope.editBase = function(baseId) {
        $state.go("knowledgeEdit",{baseId:baseId});
    };
    $scope.myVar = true;
    $scope.deleteId;
    $scope.deleteBase = function(id) {
        console.log(id);
        $scope.deleteId = id;
        $scope.myVar = !$scope.myVar;
    };

    $scope.deleteCanel = function() {
        $scope.myVar = !$scope.myVar;
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block' && (event.target.id != "showDiv" && event.target.id != "showDivImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeDiv = function() {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block' && (event.target.id != "showUserDiv" && event.target.id != "showUserImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeUserDiv = function() {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $scope.goChangePassowrd = function() {
        $state.go("changePassword");
    };
    $scope.logOut = function() {
        knowledgeAPI.logOut({userId:$cookies.getObject('user').id}, function(result){
            if(result.messageCode == "10000"){
                $cookies.putObject('user', "");
                $state.go("loginQR");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.deleteConfirm = function () {
        knowledgeAPI.deleteQuestionBase({baseId:$scope.deleteId}, function(result){
            if(result.messageCode == "10000"){
                knowledgeAPI.getQuestionBasesByUserId({userId:$cookies.getObject('user').id}, function(result){
                    if(result.messageCode == "10000"){
                        $scope.questionBases = result.questionBaseList
                    }else{
                        ngNotify.set(result.messageText, 'error');
                    }
                });
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
        $scope.myVar = !$scope.myVar;
    };
});
window.appModule.controller('knowledgeAddControl', function ($scope,$http,$state,$stateParams,knowledgeAPI,$cookies,ngNotify,$document) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    var pageSize = 60;
    var robotsJson = [];
    var checkedRobotPages = "";
    var isCheckAll = false;
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });

    if(":baseId" != $stateParams.baseId){
        $cookies.putObject('baseId', $stateParams.baseId);
    }
    knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:0,pageSize:pageSize}, function(result){
        if(result.messageCode == "10000"){
            if(result.pageCount > 0){
                $scope.pageIndex = result.pageIndex+1;
            }else{
                $scope.pageIndex = result.pageIndex;
            }
            $scope.pageCount = result.pageCount;
            for(var o in result.robotList){
                robotsJson.push(result.robotList[o]);
            }
            $scope.robots = result.robotList;
            checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goFirstPage = function(){
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:0,pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0 && isCheckAll == false){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }else{
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.goLastPage = function(){
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:$scope.pageCount-1,pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0 && isCheckAll == false){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }else{
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.save = function() {
        if("" == $scope.baseName || undefined == $scope.baseName){
            ngNotify.set('名称不能为空', 'warn');
            return;
        }else if($scope.baseName.length > 40){
            ngNotify.set('名称长度不能超过40个字', 'warn');
            return;
        }
        var isCheckAll = document.getElementById('isCheckAll').checked;
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                save(isCheckAll);
            }
        });
    };
    $scope.checkAll = function() {
        isCheckAll = document.getElementById('isCheckAll').checked;
        var checkBoxs = document.getElementsByName("robot");
        if(isCheckAll){
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = true;
            }
            knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:0,pageSize:9999}, function(result){
                if(result.messageCode == "10000"){
                    for(var o in result.robotList){
                        if(JSON.stringify(robotsJson).indexOf(result.robotList[o].serialNumber) < 0){
                            robotsJson.push(result.robotList[o]);
                            robotsJson[o].isRelated = true;
                        }else{
                            robotsJson[o].isRelated = true;
                        }
                    }
                }
            });
        }else{
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = false;
            }
            for(var o in robotsJson){
                robotsJson[o].isRelated = false;
            }
        }
    };
    $scope.textCheckAll = function() {
        isCheckAll = document.getElementById('isCheckAll').checked;
        if(isCheckAll){
            isCheckAll = false;
            document.getElementById('isCheckAll').checked = false;
        }else{
            isCheckAll = true;
            document.getElementById('isCheckAll').checked = true;
        }
        var checkBoxs = document.getElementsByName("robot");
        if(isCheckAll){
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = true;
            }
            knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:0,pageSize:9999}, function(result){
                if(result.messageCode == "10000"){
                    for(var o in result.robotList){
                        if(JSON.stringify(robotsJson).indexOf(result.robotList[o].serialNumber) < 0){
                            robotsJson.push(result.robotList[o]);
                            robotsJson[o].isRelated = true;
                        }else{
                            robotsJson[o].isRelated = true;
                        }
                    }
                }
            });
        }else{
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = false;
            }
            for(var o in robotsJson){
                robotsJson[o].isRelated = false;
            }
        }
    };
    function save(isCheckAll){
        knowledgeAPI.addQuestionBase({userId:$cookies.getObject('user').id,baseName:$scope.baseName,isCheckAll:isCheckAll,robotsJson:robotsJson}, function(result){
            if(result.messageCode == "10000"){
                $state.go("knowledgeList");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    }
    $scope.checkRobot = function(serialNumber) {
        var isCheck = document.getElementById(serialNumber);
        var allChecked = true;
        for(var o in robotsJson){
            if(robotsJson[o].serialNumber == serialNumber){
                if(robotsJson[o].isRelated){
                    robotsJson[o].isRelated = false;
                    isCheck.checked = false;
                    document.getElementById('isCheckAll').checked = false;
                }else{
                    robotsJson[o].isRelated = true;
                    isCheck.checked = true;
                }
            }
            if(robotsJson[o].isRelated == false){
                allChecked = false;
            }
        }
        if(allChecked){
            isCheck.checked = true;
            document.getElementById('isCheckAll').checked = true;
        }
    };
    $scope.goPage = function(page) {
        if($scope.pageIndex+page == -1 || $scope.pageIndex+page >= $scope.pageCount){
            return false;
        }
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:($scope.pageIndex+page),pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }
                if((checkedRobotPages.indexOf(result.pageIndex) >= 0)){
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    knowledgeAPI.getUserById({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.title = result.user.nickname;
            if(result.user.nickname.length > 8){
                result.user.nickname = result.user.nickname.substring(0,8)+"...";
            }else if(result.user.nickname.length == 0){
                result.user.nickname = result.user.phoneNumber;
            }
            if('' == result.user.imageUrl){
                result.user.imageUrl = "resource/images/UserAvatar.png";
            }
            $scope.user = result.user;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    knowledgeAPI.getMenusByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.menus = result.menuList;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goSecondMenu = function(menuId) {
        if(1 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("robotList");
                }
            });
        }else if(2 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("knowledgeList");
                }
            });
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block' && (event.target.id != "showDiv" && event.target.id != "showDivImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeDiv = function() {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block' && (event.target.id != "showUserDiv" && event.target.id != "showUserImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeUserDiv = function() {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $scope.goChangePassowrd = function() {
        $state.go("changePassword");
    };
    $scope.logOut = function() {
        knowledgeAPI.logOut({userId:$cookies.getObject('user').id}, function(result){
            if(result.messageCode == "10000"){
                $cookies.putObject('user', "");
                $state.go("loginQR");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
});
window.appModule.controller('knowledgeEditControl', function ($scope,$http,$state,$stateParams,knowledgeAPI,$cookies,ngNotify,$document) {
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    var pageSize = 60;
    var robotsJson = [];
    var checkedRobotPages = "";
    var isCheckAll = false;
    $scope.$watch('online', function(newStatus) {
        if(newStatus==false){
            ngNotify.set('网络已断开', 'warn');
        }
    });
    if(":baseId" != $stateParams.baseId){
        $cookies.putObject('baseId', $stateParams.baseId);
    };

    knowledgeAPI.getQuestionBaseById({baseId:$cookies.getObject('baseId')}, function(result){
        if(result.messageCode == "10000"){
            $scope.baseName = result.questionBase.baseName;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:pageSize}, function(result){
        if(result.messageCode == "10000"){
            if(result.pageCount > 0){
                $scope.pageIndex = result.pageIndex+1;
            }else{
                $scope.pageIndex = result.pageIndex;
            }
            $scope.pageCount = result.pageCount;
            for(var o in result.robotList){
                robotsJson.push(result.robotList[o]);
            }
            if(result.isCheckAll){
                isCheckAll = true;
                document.getElementById('isCheckAll').checked = true;
            }
            $scope.robots = result.robotList;
            checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goFirstPage = function(){
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0 && isCheckAll == false){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }else{
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.goLastPage = function(){
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,baseId:$cookies.getObject('baseId'),pageIndex:$scope.pageCount-1,pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0 && isCheckAll == false){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }else{
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.checkAll = function() {
        isCheckAll = document.getElementById('isCheckAll').checked;
        var checkBoxs = document.getElementsByName("robot");
        if(isCheckAll){
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = true;
            }
            knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,baseId:$cookies.getObject('baseId'),pageIndex:0,pageSize:9999}, function(result){
                if(result.messageCode == "10000"){
                    for(var o in result.robotList){
                        if(JSON.stringify(robotsJson).indexOf(result.robotList[o].serialNumber) < 0){
                            robotsJson.push(result.robotList[o]);
                            robotsJson[o].isRelated = true;
                        }else{
                            robotsJson[o].isRelated = true;
                        }
                    }
                }
            });
        }else{
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = false;
            }
            for(var o in robotsJson){
                robotsJson[o].isRelated = false;
            }
        }
    };
    $scope.textCheckAll = function() {
        isCheckAll = document.getElementById('isCheckAll').checked;
        if(isCheckAll){
            isCheckAll = false;
            document.getElementById('isCheckAll').checked = false;
        }else{
            isCheckAll = true;
            document.getElementById('isCheckAll').checked = true;
        }
        var checkBoxs = document.getElementsByName("robot");
        if(isCheckAll){
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = true;
            }
            knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,pageIndex:0,pageSize:9999}, function(result){
                if(result.messageCode == "10000"){
                    for(var o in result.robotList){
                        if(JSON.stringify(robotsJson).indexOf(result.robotList[o].serialNumber) < 0){
                            robotsJson.push(result.robotList[o]);
                            robotsJson[o].isRelated = true;
                        }else{
                            robotsJson[o].isRelated = true;
                        }
                    }
                }
            });
        }else{
            for (var i = 0; i < checkBoxs.length; i++) {
                checkBoxs[i].checked = false;
            }
            for(var o in robotsJson){
                robotsJson[o].isRelated = false;
            }
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $scope.update = function() {
        if("" == $scope.baseName || undefined == $scope.baseName){
            ngNotify.set('名称不能为空', 'warn');
            return;
        }else if($scope.baseName.length > 40){
            ngNotify.set('名称长度不能超过40个字', 'warn');
            return;
        }
        var isCheckAll = document.getElementById('isCheckAll').checked;
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                update(isCheckAll);
            }
        });
    };
    function update(isCheckAll){
        knowledgeAPI.updateQuestionBase({baseId:$cookies.getObject('baseId'),baseName:$scope.baseName,isCheckAll:isCheckAll,robotsJson:robotsJson}, function(result){
            if(result.messageCode == "10000"){
                $state.go("knowledgeList");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    $scope.checkRobot = function(serialNumber) {
        var isCheck = document.getElementById(serialNumber);
        var allChecked = true;
        for(var o in robotsJson){
            if(robotsJson[o].serialNumber == serialNumber){
                if(robotsJson[o].isRelated){
                    robotsJson[o].isRelated = false;
                    isCheck.checked = false;
                    document.getElementById('isCheckAll').checked = false;
                }else{
                    robotsJson[o].isRelated = true;
                    isCheck.checked = true;
                }
            }
            if(robotsJson[o].isRelated == false){
                allChecked = false;
            }
        }
        if(allChecked){
            isCheck.checked = true;
            document.getElementById('isCheckAll').checked = true;
        }
    };
    $scope.goPage = function(page) {
        if($scope.pageIndex+page == -1 || $scope.pageIndex+page >= $scope.pageCount){
            return false;
        }
        knowledgeAPI.getUserRelativeRobots({userId:$cookies.getObject('user').id,baseId:$cookies.getObject('baseId'),pageIndex:($scope.pageIndex+page),pageSize:pageSize}, function(result){
            if(result.messageCode == "10000"){
                if(result.pageCount > 0){
                    $scope.pageIndex = result.pageIndex+1;
                }else{
                    $scope.pageIndex = result.pageIndex;
                }
                $scope.pageCount = result.pageCount;
                if(checkedRobotPages.indexOf(result.pageIndex) < 0){
                    for(var o in result.robotList){
                        robotsJson.push(result.robotList[o]);
                    }
                    checkedRobotPages = checkedRobotPages+"page"+result.pageIndex+";";
                }
                if((checkedRobotPages.indexOf(result.pageIndex) >= 0)){
                    for(var o in result.robotList) {
                        for (var i in robotsJson) {
                            if (result.robotList[o].serialNumber == robotsJson[i].serialNumber) {
                                result.robotList[o].isRelated = robotsJson[i].isRelated;
                            }
                        }
                    }
                }
                $scope.robots = result.robotList;
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
    knowledgeAPI.getUserById({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.title = result.user.nickname;
            if(result.user.nickname.length > 8){
                result.user.nickname = result.user.nickname.substring(0,8)+"...";
            }else if(result.user.nickname.length == 0){
                result.user.nickname = result.user.phoneNumber;
            }
            if('' == result.user.imageUrl){
                result.user.imageUrl = "resource/images/UserAvatar.png";
            }
            $scope.user = result.user;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    knowledgeAPI.getMenusByUserId({userId:$cookies.getObject('user').id}, function(result){
        if(result.messageCode == "10000"){
            $scope.menus = result.menuList;
        }else{
            ngNotify.set(result.messageText, 'error');
        }
    });
    $scope.goSecondMenu = function(menuId) {
        if(1 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("robotList");
                }
            });
        }else if(2 == menuId){
            $scope.$watch('online', function(newStatus) {
                if(newStatus==false){
                    ngNotify.set('网络已断开', 'warn');
                }else{
                    $state.go("knowledgeList");
                }
            });
        }
    };
    $scope.goMenu = function(menuId) {
        $scope.$watch('online', function(newStatus) {
            if(newStatus==false){
                ngNotify.set('网络已断开', 'warn');
            }else{
                $state.go("menu");
            }
        });
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block' && (event.target.id != "showDiv" && event.target.id != "showDivImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeDiv = function() {
        var div = document.getElementById('hideDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $document.bind("click", function(event) {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block' && (event.target.id != "showUserDiv" && event.target.id != "showUserImg")){
            div.style.display = 'none';
        }
    });
    $scope.changeUserDiv = function() {
        var div = document.getElementById('hideUserDiv');
        if(div.style.display == 'block'){
            div.style.display = 'none';
        }else{
            div.style.display = 'block';
        }
    };
    $scope.goChangePassowrd = function() {
        $state.go("changePassword");
    };
    $scope.logOut = function() {
        knowledgeAPI.logOut({userId:$cookies.getObject('user').id}, function(result){
            if(result.messageCode == "10000"){
                $cookies.putObject('user', "");
                $state.go("loginQR");
            }else{
                ngNotify.set(result.messageText, 'error');
            }
        });
    };
});
